{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline\n",
    "import warnings\n",
    "warnings.filterwarnings(\"ignore\")\n",
    "plt.rcParams[\"font.sans-serif\"] = [\"SimHei\"]\n",
    "plt.rcParams[\"axes.unicode_minus\"] = False\n",
    "pd.options.display.max_rows = 20"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from dataclasses import dataclass\n",
    "from datetime import datetime"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "RESULT_PATH = r\"C:\\Users\\新田草\\Desktop\\正仁量化\\data\\results_all.csv\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_data(file_path, func=open) -> pd.DataFrame:\n",
    "    \"\"\"\n",
    "    从文件中 IO 获取数据\n",
    "    :param file_path:\n",
    "    :param func:\n",
    "    :return:\n",
    "    \"\"\"\n",
    "    with func(file_path) as f:\n",
    "        data = pd.read_csv(f)\n",
    "    return data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = get_data(RESULT_PATH)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = data.iloc[:, 1:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Stock</th>\n",
       "      <th>Date</th>\n",
       "      <th>Time</th>\n",
       "      <th>Timestamp</th>\n",
       "      <th>Tradeprice</th>\n",
       "      <th>Tradevolume</th>\n",
       "      <th>Tradevalue</th>\n",
       "      <th>Direction</th>\n",
       "      <th>Timestamp_time</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>CY1805</td>\n",
       "      <td>20180102</td>\n",
       "      <td>90713391</td>\n",
       "      <td>1514855233</td>\n",
       "      <td>23205</td>\n",
       "      <td>1</td>\n",
       "      <td>116025</td>\n",
       "      <td>B</td>\n",
       "      <td>2018-01-02 09:07:13+08:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>CY1805</td>\n",
       "      <td>20180102</td>\n",
       "      <td>90713391</td>\n",
       "      <td>1514855233</td>\n",
       "      <td>23215</td>\n",
       "      <td>1</td>\n",
       "      <td>116075</td>\n",
       "      <td>B</td>\n",
       "      <td>2018-01-02 09:07:13+08:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>CY1805</td>\n",
       "      <td>20180102</td>\n",
       "      <td>91019098</td>\n",
       "      <td>1514855419</td>\n",
       "      <td>23215</td>\n",
       "      <td>4</td>\n",
       "      <td>464300</td>\n",
       "      <td>S</td>\n",
       "      <td>2018-01-02 09:10:19+08:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>CY1805</td>\n",
       "      <td>20180102</td>\n",
       "      <td>91300195</td>\n",
       "      <td>1514855580</td>\n",
       "      <td>23205</td>\n",
       "      <td>1</td>\n",
       "      <td>116025</td>\n",
       "      <td>S</td>\n",
       "      <td>2018-01-02 09:13:00+08:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>CY1805</td>\n",
       "      <td>20180102</td>\n",
       "      <td>91300195</td>\n",
       "      <td>1514855580</td>\n",
       "      <td>23195</td>\n",
       "      <td>1</td>\n",
       "      <td>115975</td>\n",
       "      <td>S</td>\n",
       "      <td>2018-01-02 09:13:00+08:00</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    Stock      Date      Time   Timestamp  Tradeprice  Tradevolume  \\\n",
       "0  CY1805  20180102  90713391  1514855233       23205            1   \n",
       "1  CY1805  20180102  90713391  1514855233       23215            1   \n",
       "2  CY1805  20180102  91019098  1514855419       23215            4   \n",
       "3  CY1805  20180102  91300195  1514855580       23205            1   \n",
       "4  CY1805  20180102  91300195  1514855580       23195            1   \n",
       "\n",
       "   Tradevalue Direction             Timestamp_time  \n",
       "0      116025         B  2018-01-02 09:07:13+08:00  \n",
       "1      116075         B  2018-01-02 09:07:13+08:00  \n",
       "2      464300         S  2018-01-02 09:10:19+08:00  \n",
       "3      116025         S  2018-01-02 09:13:00+08:00  \n",
       "4      115975         S  2018-01-02 09:13:00+08:00  "
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = data.sort_values(by=\"Timestamp_time\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Stock</th>\n",
       "      <th>Date</th>\n",
       "      <th>Time</th>\n",
       "      <th>Timestamp</th>\n",
       "      <th>Tradeprice</th>\n",
       "      <th>Tradevolume</th>\n",
       "      <th>Tradevalue</th>\n",
       "      <th>Direction</th>\n",
       "      <th>Timestamp_time</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>CY1805</td>\n",
       "      <td>20180102</td>\n",
       "      <td>90713391</td>\n",
       "      <td>1514855233</td>\n",
       "      <td>23205</td>\n",
       "      <td>1</td>\n",
       "      <td>116025</td>\n",
       "      <td>B</td>\n",
       "      <td>2018-01-02 09:07:13+08:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>CY1805</td>\n",
       "      <td>20180102</td>\n",
       "      <td>90713391</td>\n",
       "      <td>1514855233</td>\n",
       "      <td>23215</td>\n",
       "      <td>1</td>\n",
       "      <td>116075</td>\n",
       "      <td>B</td>\n",
       "      <td>2018-01-02 09:07:13+08:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>CY1805</td>\n",
       "      <td>20180102</td>\n",
       "      <td>91019098</td>\n",
       "      <td>1514855419</td>\n",
       "      <td>23215</td>\n",
       "      <td>4</td>\n",
       "      <td>464300</td>\n",
       "      <td>S</td>\n",
       "      <td>2018-01-02 09:10:19+08:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>CY1805</td>\n",
       "      <td>20180102</td>\n",
       "      <td>91300195</td>\n",
       "      <td>1514855580</td>\n",
       "      <td>23205</td>\n",
       "      <td>1</td>\n",
       "      <td>116025</td>\n",
       "      <td>S</td>\n",
       "      <td>2018-01-02 09:13:00+08:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>CY1805</td>\n",
       "      <td>20180102</td>\n",
       "      <td>91300195</td>\n",
       "      <td>1514855580</td>\n",
       "      <td>23195</td>\n",
       "      <td>1</td>\n",
       "      <td>115975</td>\n",
       "      <td>S</td>\n",
       "      <td>2018-01-02 09:13:00+08:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>CY1805</td>\n",
       "      <td>20180102</td>\n",
       "      <td>91300195</td>\n",
       "      <td>1514855580</td>\n",
       "      <td>23200</td>\n",
       "      <td>2</td>\n",
       "      <td>232000</td>\n",
       "      <td>S</td>\n",
       "      <td>2018-01-02 09:13:00+08:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>CY1805</td>\n",
       "      <td>20180102</td>\n",
       "      <td>91309200</td>\n",
       "      <td>1514855589</td>\n",
       "      <td>23205</td>\n",
       "      <td>1</td>\n",
       "      <td>116025</td>\n",
       "      <td>S</td>\n",
       "      <td>2018-01-02 09:13:09+08:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>CY1805</td>\n",
       "      <td>20180102</td>\n",
       "      <td>91309200</td>\n",
       "      <td>1514855589</td>\n",
       "      <td>23215</td>\n",
       "      <td>1</td>\n",
       "      <td>116075</td>\n",
       "      <td>S</td>\n",
       "      <td>2018-01-02 09:13:09+08:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>CY1805</td>\n",
       "      <td>20180102</td>\n",
       "      <td>91310256</td>\n",
       "      <td>1514855590</td>\n",
       "      <td>23205</td>\n",
       "      <td>1</td>\n",
       "      <td>116025</td>\n",
       "      <td>S</td>\n",
       "      <td>2018-01-02 09:13:10+08:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>CY1805</td>\n",
       "      <td>20180102</td>\n",
       "      <td>91310256</td>\n",
       "      <td>1514855590</td>\n",
       "      <td>23175</td>\n",
       "      <td>1</td>\n",
       "      <td>115875</td>\n",
       "      <td>S</td>\n",
       "      <td>2018-01-02 09:13:10+08:00</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    Stock      Date      Time   Timestamp  Tradeprice  Tradevolume  \\\n",
       "0  CY1805  20180102  90713391  1514855233       23205            1   \n",
       "1  CY1805  20180102  90713391  1514855233       23215            1   \n",
       "2  CY1805  20180102  91019098  1514855419       23215            4   \n",
       "3  CY1805  20180102  91300195  1514855580       23205            1   \n",
       "4  CY1805  20180102  91300195  1514855580       23195            1   \n",
       "5  CY1805  20180102  91300195  1514855580       23200            2   \n",
       "6  CY1805  20180102  91309200  1514855589       23205            1   \n",
       "7  CY1805  20180102  91309200  1514855589       23215            1   \n",
       "8  CY1805  20180102  91310256  1514855590       23205            1   \n",
       "9  CY1805  20180102  91310256  1514855590       23175            1   \n",
       "\n",
       "   Tradevalue Direction             Timestamp_time  \n",
       "0      116025         B  2018-01-02 09:07:13+08:00  \n",
       "1      116075         B  2018-01-02 09:07:13+08:00  \n",
       "2      464300         S  2018-01-02 09:10:19+08:00  \n",
       "3      116025         S  2018-01-02 09:13:00+08:00  \n",
       "4      115975         S  2018-01-02 09:13:00+08:00  \n",
       "5      232000         S  2018-01-02 09:13:00+08:00  \n",
       "6      116025         S  2018-01-02 09:13:09+08:00  \n",
       "7      116075         S  2018-01-02 09:13:09+08:00  \n",
       "8      116025         S  2018-01-02 09:13:10+08:00  \n",
       "9      115875         S  2018-01-02 09:13:10+08:00  "
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.head(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [],
   "source": [
    "@dataclass\n",
    "class BarData(object):\n",
    "    \"\"\"\n",
    "    Candlestick bar data of a certain price movement.\n",
    "    \"\"\"\n",
    "    contract: str\n",
    "    datetime: datetime\n",
    "    \n",
    "    Timestamp: float = 0\n",
    "    volume: float = 0\n",
    "    open_price: float = 0\n",
    "    high_price: float = 0\n",
    "    low_price: float = 0\n",
    "    close_price: float = 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [],
   "source": [
    "class ColsIndex(object):\n",
    "    \"\"\"\n",
    "    列索引容器\n",
    "    \"\"\"\n",
    "    def __init__(self):\n",
    "        self.Stock = None\n",
    "        self.Timestamp_time_ind = None\n",
    "        self.Tradeprice_ind = None\n",
    "        self.Tradevolume_ind = None\n",
    "        self.Timestamp_ind = None\n",
    "        \n",
    "def get_col_index(columns: list):\n",
    "    cols_index = ColsIndex()\n",
    "    for ind, each in enumerate(columns):\n",
    "        if each == \"Stock\":\n",
    "            cols_index.Stock = ind\n",
    "        if each == \"Timestamp_time\":\n",
    "            cols_index.Timestamp_time_ind = ind\n",
    "        if each == \"Timestamp\":\n",
    "            cols_index.Timestamp_ind = ind\n",
    "        if each == \"Tradeprice\":\n",
    "            cols_index.Tradeprice_ind = ind\n",
    "        if each == \"Tradevolume\":\n",
    "            cols_index.Tradevolume_ind = ind\n",
    "        \n",
    "    return cols_index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [],
   "source": [
    "data_cols = data.columns\n",
    "data_cols_index = get_col_index(data_cols)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'Stock': 0,\n",
       " 'Timestamp_time_ind': 8,\n",
       " 'Tradeprice_ind': 4,\n",
       " 'Tradevolume_ind': 5,\n",
       " 'Timestamp_ind': 3}"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_cols_index.__dict__"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([['CY1805', 20180102, 90713391, ..., 116025, 'B',\n",
       "        '2018-01-02 09:07:13+08:00'],\n",
       "       ['CY1805', 20180102, 90713391, ..., 116075, 'B',\n",
       "        '2018-01-02 09:07:13+08:00'],\n",
       "       ['CY1805', 20180102, 91019098, ..., 464300, 'S',\n",
       "        '2018-01-02 09:10:19+08:00'],\n",
       "       ...,\n",
       "       ['CY1805', 20180410, 222853010, ..., -115325, 'S',\n",
       "        '2018-04-10 22:28:53+08:00'],\n",
       "       ['CY1805', 20180418, 112700125, ..., 116725, 'B',\n",
       "        '2018-04-18 11:27:00+08:00'],\n",
       "       ['CY1805', 20180418, 112700125, ..., -116725, 'B',\n",
       "        '2018-04-18 11:27:00+08:00']], dtype=object)"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "int"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(data.values[0][data_cols_index.Tradevolume_ind])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {},
   "outputs": [],
   "source": [
    "def bar_generator(data: pd.DataFrame, price_distance: int=100) -> list:\n",
    "    \n",
    "    tick_datas = data.values\n",
    "    \n",
    "    # 初始化 open_price\n",
    "    open_price = tick_datas[0][data_cols_index.Tradeprice_ind]\n",
    "    high_price = 0.0\n",
    "    low_price = np.inf\n",
    "    close_price = 0.0\n",
    "    # 初始化 volume_trade\n",
    "    volume_trade = int(tick_datas[0][data_cols_index.Tradevolume_ind])\n",
    "    \n",
    "    bar_container = list()\n",
    "    \n",
    "    # 逐笔成交数据推送\n",
    "    for TickData in tick_datas[1: ]:\n",
    "        \n",
    "        last_price = TickData[data_cols_index.Tradeprice_ind]\n",
    "        \n",
    "        # 更新 volume_trade\n",
    "        volume_trade += int(TickData[data_cols_index.Tradevolume_ind])\n",
    "\n",
    "        # 更新 high_price\n",
    "        if high_price < last_price:\n",
    "            high_price = last_price\n",
    "        # 更新 low_price\n",
    "        if low_price > last_price:\n",
    "            low_price = last_price\n",
    "\n",
    "        if (last_price - open_price) >= price_distance:\n",
    "            # 更新 close_price\n",
    "            close_price = last_price\n",
    "\n",
    "            # 生成一个 Bar\n",
    "            bar = BarData(close_price = close_price, \n",
    "                         open_price = open_price, \n",
    "                         low_price = low_price, \n",
    "                         high_price = high_price, \n",
    "                         volume = volume_trade, \n",
    "                         contract = TickData[data_cols_index.Stock], \n",
    "                         datetime=TickData[data_cols_index.Timestamp_time_ind], \n",
    "                         Timestamp=TickData[data_cols_index.Timestamp_ind])\n",
    "            # 存储新生成的Bar\n",
    "            bar_container.append(bar.__dict__)\n",
    "            \n",
    "            open_price = last_price\n",
    "            high_price = 0.0\n",
    "            low_price = np.inf\n",
    "            close_price = 0.0\n",
    "            volume_trade = 0\n",
    "    \n",
    "    return bar_container\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [],
   "source": [
    "bars = bar_generator(data, 30)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'contract': 'CY1805',\n",
       " 'datetime': '2018-01-09 14:56:01+08:00',\n",
       " 'Timestamp': 1515480961,\n",
       " 'volume': 184,\n",
       " 'open_price': 23205,\n",
       " 'high_price': 23260,\n",
       " 'low_price': 22915,\n",
       " 'close_price': 23260}"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bars[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    ", columns="
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {},
   "outputs": [],
   "source": [
    "results = pd.DataFrame.from_dict(bars)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {},
   "outputs": [],
   "source": [
    "results = results[[\"contract\", \"datetime\", \"Timestamp\", \"open_price\", \n",
    "                                      \"high_price\", \"low_price\", \"close_price\", \"volume\"]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>contract</th>\n",
       "      <th>datetime</th>\n",
       "      <th>Timestamp</th>\n",
       "      <th>open_price</th>\n",
       "      <th>high_price</th>\n",
       "      <th>low_price</th>\n",
       "      <th>close_price</th>\n",
       "      <th>volume</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>CY1805</td>\n",
       "      <td>2018-01-09 14:56:01+08:00</td>\n",
       "      <td>1515480961</td>\n",
       "      <td>23205</td>\n",
       "      <td>23260</td>\n",
       "      <td>22915</td>\n",
       "      <td>23260</td>\n",
       "      <td>184</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>CY1805</td>\n",
       "      <td>2018-01-12 11:10:08+08:00</td>\n",
       "      <td>1515726608</td>\n",
       "      <td>23260</td>\n",
       "      <td>23300</td>\n",
       "      <td>23065</td>\n",
       "      <td>23300</td>\n",
       "      <td>35</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>CY1805</td>\n",
       "      <td>2018-01-18 13:43:54+08:00</td>\n",
       "      <td>1516254234</td>\n",
       "      <td>23300</td>\n",
       "      <td>23345</td>\n",
       "      <td>23030</td>\n",
       "      <td>23345</td>\n",
       "      <td>92</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>CY1805</td>\n",
       "      <td>2018-01-18 13:50:18+08:00</td>\n",
       "      <td>1516254618</td>\n",
       "      <td>23345</td>\n",
       "      <td>23440</td>\n",
       "      <td>23235</td>\n",
       "      <td>23440</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>CY1805</td>\n",
       "      <td>2018-01-23 10:44:17+08:00</td>\n",
       "      <td>1516675457</td>\n",
       "      <td>23440</td>\n",
       "      <td>23500</td>\n",
       "      <td>23120</td>\n",
       "      <td>23500</td>\n",
       "      <td>141</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>CY1805</td>\n",
       "      <td>2018-01-23 10:58:43+08:00</td>\n",
       "      <td>1516676323</td>\n",
       "      <td>23500</td>\n",
       "      <td>23530</td>\n",
       "      <td>23280</td>\n",
       "      <td>23530</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>CY1805</td>\n",
       "      <td>2018-01-23 11:09:25+08:00</td>\n",
       "      <td>1516676965</td>\n",
       "      <td>23530</td>\n",
       "      <td>23590</td>\n",
       "      <td>23280</td>\n",
       "      <td>23590</td>\n",
       "      <td>16</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>CY1805</td>\n",
       "      <td>2018-01-23 11:24:06+08:00</td>\n",
       "      <td>1516677846</td>\n",
       "      <td>23590</td>\n",
       "      <td>23620</td>\n",
       "      <td>23385</td>\n",
       "      <td>23620</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>CY1805</td>\n",
       "      <td>2018-01-23 14:39:39+08:00</td>\n",
       "      <td>1516689579</td>\n",
       "      <td>23620</td>\n",
       "      <td>23690</td>\n",
       "      <td>23255</td>\n",
       "      <td>23690</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>CY1805</td>\n",
       "      <td>2018-01-23 14:53:00+08:00</td>\n",
       "      <td>1516690380</td>\n",
       "      <td>23690</td>\n",
       "      <td>23785</td>\n",
       "      <td>23350</td>\n",
       "      <td>23785</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  contract                   datetime   Timestamp  open_price  high_price  \\\n",
       "0   CY1805  2018-01-09 14:56:01+08:00  1515480961       23205       23260   \n",
       "1   CY1805  2018-01-12 11:10:08+08:00  1515726608       23260       23300   \n",
       "2   CY1805  2018-01-18 13:43:54+08:00  1516254234       23300       23345   \n",
       "3   CY1805  2018-01-18 13:50:18+08:00  1516254618       23345       23440   \n",
       "4   CY1805  2018-01-23 10:44:17+08:00  1516675457       23440       23500   \n",
       "5   CY1805  2018-01-23 10:58:43+08:00  1516676323       23500       23530   \n",
       "6   CY1805  2018-01-23 11:09:25+08:00  1516676965       23530       23590   \n",
       "7   CY1805  2018-01-23 11:24:06+08:00  1516677846       23590       23620   \n",
       "8   CY1805  2018-01-23 14:39:39+08:00  1516689579       23620       23690   \n",
       "9   CY1805  2018-01-23 14:53:00+08:00  1516690380       23690       23785   \n",
       "\n",
       "   low_price  close_price  volume  \n",
       "0      22915        23260     184  \n",
       "1      23065        23300      35  \n",
       "2      23030        23345      92  \n",
       "3      23235        23440       2  \n",
       "4      23120        23500     141  \n",
       "5      23280        23530       5  \n",
       "6      23280        23590      16  \n",
       "7      23385        23620       6  \n",
       "8      23255        23690       5  \n",
       "9      23350        23785       4  "
      ]
     },
     "execution_count": 76,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "metadata": {},
   "outputs": [],
   "source": [
    "results.columns = [\"contract\", \"time\", \"Timestamp\", \"open\", \"high\", \"low\", \"close\", \"volume\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>contract</th>\n",
       "      <th>time</th>\n",
       "      <th>Timestamp</th>\n",
       "      <th>open</th>\n",
       "      <th>high</th>\n",
       "      <th>low</th>\n",
       "      <th>close</th>\n",
       "      <th>volume</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>CY1805</td>\n",
       "      <td>2018-01-09 14:56:01+08:00</td>\n",
       "      <td>1515480961</td>\n",
       "      <td>23205</td>\n",
       "      <td>23260</td>\n",
       "      <td>22915</td>\n",
       "      <td>23260</td>\n",
       "      <td>184</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>CY1805</td>\n",
       "      <td>2018-01-12 11:10:08+08:00</td>\n",
       "      <td>1515726608</td>\n",
       "      <td>23260</td>\n",
       "      <td>23300</td>\n",
       "      <td>23065</td>\n",
       "      <td>23300</td>\n",
       "      <td>35</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>CY1805</td>\n",
       "      <td>2018-01-18 13:43:54+08:00</td>\n",
       "      <td>1516254234</td>\n",
       "      <td>23300</td>\n",
       "      <td>23345</td>\n",
       "      <td>23030</td>\n",
       "      <td>23345</td>\n",
       "      <td>92</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>CY1805</td>\n",
       "      <td>2018-01-18 13:50:18+08:00</td>\n",
       "      <td>1516254618</td>\n",
       "      <td>23345</td>\n",
       "      <td>23440</td>\n",
       "      <td>23235</td>\n",
       "      <td>23440</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>CY1805</td>\n",
       "      <td>2018-01-23 10:44:17+08:00</td>\n",
       "      <td>1516675457</td>\n",
       "      <td>23440</td>\n",
       "      <td>23500</td>\n",
       "      <td>23120</td>\n",
       "      <td>23500</td>\n",
       "      <td>141</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>CY1805</td>\n",
       "      <td>2018-01-23 10:58:43+08:00</td>\n",
       "      <td>1516676323</td>\n",
       "      <td>23500</td>\n",
       "      <td>23530</td>\n",
       "      <td>23280</td>\n",
       "      <td>23530</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>CY1805</td>\n",
       "      <td>2018-01-23 11:09:25+08:00</td>\n",
       "      <td>1516676965</td>\n",
       "      <td>23530</td>\n",
       "      <td>23590</td>\n",
       "      <td>23280</td>\n",
       "      <td>23590</td>\n",
       "      <td>16</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>CY1805</td>\n",
       "      <td>2018-01-23 11:24:06+08:00</td>\n",
       "      <td>1516677846</td>\n",
       "      <td>23590</td>\n",
       "      <td>23620</td>\n",
       "      <td>23385</td>\n",
       "      <td>23620</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>CY1805</td>\n",
       "      <td>2018-01-23 14:39:39+08:00</td>\n",
       "      <td>1516689579</td>\n",
       "      <td>23620</td>\n",
       "      <td>23690</td>\n",
       "      <td>23255</td>\n",
       "      <td>23690</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>CY1805</td>\n",
       "      <td>2018-01-23 14:53:00+08:00</td>\n",
       "      <td>1516690380</td>\n",
       "      <td>23690</td>\n",
       "      <td>23785</td>\n",
       "      <td>23350</td>\n",
       "      <td>23785</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  contract                       time   Timestamp   open   high    low  close  \\\n",
       "0   CY1805  2018-01-09 14:56:01+08:00  1515480961  23205  23260  22915  23260   \n",
       "1   CY1805  2018-01-12 11:10:08+08:00  1515726608  23260  23300  23065  23300   \n",
       "2   CY1805  2018-01-18 13:43:54+08:00  1516254234  23300  23345  23030  23345   \n",
       "3   CY1805  2018-01-18 13:50:18+08:00  1516254618  23345  23440  23235  23440   \n",
       "4   CY1805  2018-01-23 10:44:17+08:00  1516675457  23440  23500  23120  23500   \n",
       "5   CY1805  2018-01-23 10:58:43+08:00  1516676323  23500  23530  23280  23530   \n",
       "6   CY1805  2018-01-23 11:09:25+08:00  1516676965  23530  23590  23280  23590   \n",
       "7   CY1805  2018-01-23 11:24:06+08:00  1516677846  23590  23620  23385  23620   \n",
       "8   CY1805  2018-01-23 14:39:39+08:00  1516689579  23620  23690  23255  23690   \n",
       "9   CY1805  2018-01-23 14:53:00+08:00  1516690380  23690  23785  23350  23785   \n",
       "\n",
       "   volume  \n",
       "0     184  \n",
       "1      35  \n",
       "2      92  \n",
       "3       2  \n",
       "4     141  \n",
       "5       5  \n",
       "6      16  \n",
       "7       6  \n",
       "8       5  \n",
       "9       4  "
      ]
     },
     "execution_count": 80,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
